home *** CD-ROM | disk | FTP | other *** search
/ The Best of MacTutor - S…e Code for Volumes 1 to 5 / The Best of MacTutor - Source Code for Volume 1-5 (Wayzata Technology)(6031)(1990).bin / Source Code / #26 (Nov 87) / asm lab Formatter / NumToString.asm < prev    next >
Assembly Source File  |  1987-10-21  |  1KB  |  59 lines

  1. ; NumToString.asm
  2. ;-----------------
  3. ; by Mike™ Scanlin  28 Dec 1986
  4. ; an alternative to _NumToString
  5.  
  6. Xref    NumToString
  7.  
  8. ;==========
  9. NumToString
  10. ;==========
  11. ; convert a 32 bit integer into a pascal string
  12. ;  input: D0 longint
  13. ;      A0 points to a space of at least 12 bytes
  14. ; output: A0 points to pascal string
  15.  
  16.     MOVEM.L        D0-D4/A1-A2,-(SP)
  17.     LEA        1(A0),A1    ;skip length byte
  18.     TST.L        D0        ;is number zero?
  19.     BGT.S        @2
  20.     BMI.S        @1
  21.     MOVE.B        #'0',(A1)+    ;special case for zero
  22.     BRA.S        @8
  23. @1    MOVE.B        #'-',(A1)+    ;give string a minus sign
  24.     NEG.L        D0        ;make number positive
  25. @2    LEA        PowersTable,A2
  26.     MOVEQ        #1,D3        ;set leading zeros flag
  27.     MOVEQ        #9,D4        ;loop counter
  28. @3    MOVE.L        (A2)+,D2    ;get a power of 10
  29.     MOVEQ        #'0',D1        ;init digit
  30. @4    CMP.L        D2,D0        ;is # > power of 10?
  31.     BLT.S        @5
  32.     ADDQ        #1,D1        ;increase digit
  33.     SUB.L        D2,D0        ;subtract power of 10
  34.     BNE.S        @4
  35. @5    TST        D3        ;have we had a non-zero digit yet?
  36.     BEQ.S        @6
  37.     CMP.B        #'0',D1        ;is this a leading zero?
  38.     BEQ.S        @7
  39.     MOVEQ        #0,D3        ;print all zeros from now on
  40. @6    MOVE.B        D1,(A1)+
  41. @7    DBRA        D4,@3
  42. @8    MOVE        A1,D0        ;calc length of new string
  43.     SUB        A0,D0
  44.     SUBQ.B        #1,D0        ;minus 1 for length byte
  45.     MOVE.B        D0,(A0)
  46.     MOVEM.L        (SP)+,A1-A2/D0-D4
  47.     RTS
  48.  
  49. PowersTable    DC.L    1000000000
  50.         DC.L    100000000
  51.         DC.L    10000000
  52.         DC.L    1000000
  53.         DC.L    100000
  54.         DC.L    10000
  55.         DC.L    1000
  56.         DC.L    100
  57.         DC.L    10
  58.         DC.L    1
  59.